import hashlib
import json

import jwt
from django.core.paginator import Paginator
from django.forms.models import model_to_dict
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods

from .models import User,tushufenlei,tushu
def jiemi(pwd):
    return hashlib.md5(pwd.encode()).hexdigest()
def is_empty(c):
    return c is None or str(c).strip()==""
@csrf_exempt
@require_http_methods(["POST"])
def login(request):
    data=json.loads(request.body)
    username=data["username"]
    password=data["password"]
    if is_empty(username) or is_empty(password):
        return JsonResponse({"error":"用户名或密码为空"})
    if User.objects.filter(username=username).exists()==False:
        return JsonResponse({"error":"用户名不正确"})
    usr=User.objects.get(username=username)
    if usr.password!=jiemi(password):
        return JsonResponse({"error":"密码不正确"})
    pay={"usr_id":usr.username,"enum":usr.enum,"user_id":usr.id}
    mi="159357"
    token=jwt.encode(pay,mi)
    return JsonResponse({"message":"登录成功","token":token})
@require_http_methods(["PUT"])
def unique(request):
    data=json.loads(request.body)
    id=data["id"]
    tuname=data["tuname"]
    price=data["price"]
    num=data["num"]
    createxgtime=data["createxgtime"]
    fenid_id = data["fenid_id"]
    if num<=5:
        return JsonResponse({"message":"库存不足"})
    xiu=tushu.objects.get(id=id)
    xiu.tuname=tuname
    xiu.price=price
    xiu.num=num
    xiu.createxgtime=createxgtime
    xiu.fenid_id=fenid_id
    xiu.save()
    return JsonResponse({"message":"修改成功"})
@require_http_methods(["GET"])
def select(request):
    mi="159357"
    token=request.META.get('HTTP_TOKEN')
    if not token:
        return JsonResponse({"error":"请先登录"})
    try:
        payy=jwt.decode(token,mi,algorithms=["HS256"])
    except:
        return JsonResponse("token无效")
    all_data=tushu.objects.all()
    page=request.GET.get("page",1)
    page_size=request.GET.get("page_size")or 10
    tuname=request.GET.get("tuname","")
    price=request.GET.get("price","")
    if tuname:
        all_data=tushu.objects.filter(tuname=tuname)
    if price:
        all_data=tushu.objects.filter(price=price)
    l=len(all_data)
    pa=Paginator(all_data,page_size)
    table_data=pa.get_page(page)
    list=[]

    for intem in table_data:
        list.append({
            "id":intem.id,
            "tuname":intem.tuname,
            "price":intem.price,
            "num":intem.num,
            "createxgtime":intem.createxgtime,

        })
    return JsonResponse({"message":"查询成功","data":list,"total":l})
@require_http_methods(["DELETE"])
def delete(request,id):
    mi = "159357"
    token = request.META.get('HTTP_TOKEN')
    if not token:
        return JsonResponse({"error": "请先登录"})
    try:
        payy = jwt.decode(token, mi, algorithms=["HS256"])
    except:
        return JsonResponse("token无效")
    sc=tushu.objects.get(id=id)
    sc.delete()



    return JsonResponse({"message":"删除成功"})

@require_http_methods(["POST"])
def add(request):
    mi = "159357"
    token = request.META.get('HTTP_TOKEN')
    if not token:
        return JsonResponse({"error": "请先登录"})
    try:
        payy = jwt.decode(token, mi, algorithms=["HS256"])
    except:
        return JsonResponse("token无效")
    data=json.loads(request.body)
    tuname=data["tuname"]
    price=data["price"]
    num=data["num"]
    createxgtime=data["createxgtime"]
    fenid_id=data["fenid_id"]
    crat=tushu.objects.create(
        tuname=tuname,
        price=price,
        num=num,
        createxgtime=createxgtime,
        fenid_id=fenid_id

    )

    return JsonResponse({"message":"添加成功","data":model_to_dict(crat)})

#


